import express from 'express';
import sqlite3 from 'sqlite3';
import cors from 'cors';

const app = express();
const db = new sqlite3.Database('./low_atitude.sqlite3');  // 请替换为你的 SQLite 数据库路径


// NOTE:处理前端界面请求，返回数据库内容
app.use(express.json());
app.use(cors());  // 允许跨域访问

// 查询数据接口
app.post('/query', (req, res) => {
  const { airType, date } = req.body;
  // console.log("req.body : ",req.body);

  let query = "SELECT * FROM 'air_space_list' WHERE 1=1";
  const params = [];

  // 根据选项和日期构建查询条件
  if (airType) {
    // console.log('Received airType:', airType);

    query += ` AND type = '${airType}'`;
  }
  if (date) {
    // console.log('Received date:', date);

    query += ` AND createTime = ${date}`;
  }

  console.log('Query content:', query);

  db.all(query, (err, rows) => {
    if (err) {
      console.error(err);
      return res.status(500).json({ error: '查询失败' });
    }
    res.json(rows);  // 返回查询结果
  });

});

// 启动服务器
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});